`timescale 1ns/1ns
module v8_data_rec_02(
		input	wire		resetb,
		input	wire		sclk,
		
		input   wire            init_mode,
		input   wire            black,
		input   wire            test_vs,
		input   wire            local_test_en,
		input   wire            local_picture_en,
		
	        input	wire		set_d_ok,
	        input	wire	[15:0]	set_addr,
	        input	wire	[7:0]	set_data,

                output  reg     [1:0]   pixel_mode,
                
                output  reg     [7:0]   local_depth,
                output	reg		local_depth_en,
                output	reg		cas_depth_adj,
                
		input	wire		mac_flag,
		input	wire	[7:0]	mac_data,
		input	wire	[1:0]	mac_vendor,//0:无效包 1：中庆UDP 2：YT 3:中庆长包
		input	wire		mac_error,
		input	wire		yt_vs_pre,
		
		output	wire		vsout,
		
                output  reg     [10:0]  h_total_reg,
                output  reg     [10:0]  l_total_reg,
		output  wire    [10:0]  h_total,
		output  wire    [10:0]  l_total,
		
		output	reg	[10:0]	h_num,
		output	reg		hsout,
		output	wire	[7:0]	dout,
		
		output	reg	[63:0]	unit_color_adj,
		output  reg     [8:0]   cascade_light,
		output	reg	[7:0]	led_light,
		
		output	reg	[7:0]	state,
		output	reg	[7:0]	state_2,
		output	reg	[1:0]	color_restore_type,
		output	reg	[7:0]	testmode,
		
		output	reg	[7:0]	no_vs_set,	
		
		output	reg	[13:0]	dis_p_count,
		output	wire	[7:0]	tout
		);

/************************************************/
//		信号定义
/************************************************/
parameter  Preamble_OFFSET	= 23;
parameter  PACK_TYPE_FIRST	= 24 - Preamble_OFFSET;
parameter  PACK_DEPTH_FIRST	= 28 - Preamble_OFFSET;
parameter  PACK_LENGTH_HIGH	= 35 - Preamble_OFFSET;
parameter  PACK_DATA_START	= 36 - Preamble_OFFSET;
parameter  HPACK_ADDR_START	= 30 - Preamble_OFFSET;
parameter  SET_PACK_OFFSET	= PACK_DATA_START-2;

reg	[5:0]	display_num_pre,display_num;
reg	[10:0]	box_h_offset,box_l_offset;
reg             light_on_protect,local_depth_en_t;
reg	[10:0]	mac_count;
reg	[9:0]	mac_count_add;
reg	[10:0]	set_count;
reg             set_en;
reg     [3:0]   set_count_a_low;
reg		set_en_pre,data_en_pre;

reg		table_wea;
wire	[9:0]	table_addra;
wire	[9:0]	table_addrb;
wire	[7:0]	table_dina,table_douta;
wire	[15:0]	table_doutb;

reg	[11:0]	h_num_rec;
reg		h_active,h_valid;
reg	[1:0]	base_count;
reg		base_end;
reg	[12:0]	l_count_byte=6144;

reg     [11:0]  h_num_local;
reg     [11:0]  l_max_local;
reg	[11:0]	h_max_local;
wire	[10:0]	h_num_temp;

reg     [9:0]   h_l_offset;
reg     [10:0]  l_offset; 
reg	[10:0]	h_start,l_start;
wire		vir_pixel;
reg             vir_mode;
wire		vir_disp;
reg	[10:0]	display_h_offset,display_l_offset;
reg     [10:0]  h_offset_mult_result;
wire    [31:0]  l_offset_mult_result;
wire    [10:0]  box_h_offset_local,box_l_offset_local;     
wire    [31:0]  h_total_mult_result,l_total_mult_result;

wire    [31:0]  h_num_mult_result;
reg	[7:0]	contrast_adj;
reg	[6:0]	contrast_da;
wire	[17:0]	contrast_db;
reg		flag_da,flag_db,hsout_a,hsout_b;
reg		local_buf_we_r,cascade_buf_we_r,result_buf_we;
reg	[2:0]	local_buf_addr;
reg	[1:0]	cascade_buf_waddr;
reg	[8:0]	local_buf_wdata,cascade_buf_wdata;
reg     [7:0]   start_light;        
reg	[8:0]	local_buf_rdata,cascade_buf_rdata;
wire	[15:0]	result_buf_wdata;
wire	[15:0]	result_buf_rdata;
wire	[17:0]	sys_adj_result;
wire	[31:0]	adj_result_r,adj_result_g,adj_result_b,adj_result_w;
reg             test_vs_r;
reg             local_vs_negedge;
//12-5-27 kyyou
reg	[8:0]	unit_r_adj='h0100,unit_g_adj='h0100,unit_b_adj='h0100,unit_w_adj='h0100;//分色
reg	[8:0]	r_light='h0100,g_light='h0100,b_light='h0100,w_light='h0100;//色温
wire	[15:0]	r_adj_out,g_adj_out,b_adj_out,w_adj_out;

reg     [15:0]  wdata;
wire    [31:0]  rdata;
reg     [8:0]   waddr;
reg     [7:0]   raddr;
reg             wren;

//jzhang 
reg	[12:0]	l_num_rec;	//一行的数据起始字节数
reg	[12:0]	col_max;
reg	[12:0]	valid_l_byte_max,l_byte_max=6144;
reg	[11:0]	h_count;
wire	[11:0]	l_num;
wire	[7:0]	rec_dout;
wire		rec_hout;
wire	[10:0]	rec_h_num;
reg	[12:0]	l_start_byte;
reg	[12:0]	l_max_local_byte;
reg	[11:0]  dis_buf_waddr=0,dis_buf_raddr=0;
wire		dis_buf_we;
wire	[7:0]	dis_buf_wdata,dis_buf_rdata;
wire	[11:0]	l_total_byte;
reg		send_out_flag;
reg		rec_hout_t;
reg		hout_t1;
reg		zdec_vsout;
reg		zdec_rec_hout;
reg	[7:0]	zdec_rec_dout;
reg	[11:0]	zdec_l_num;
reg	[10:0]	zdec_h_num;
reg	[11:0]	zdec_l_total_byte;
wire	[11:0]	zdec_l_total_byte_t;
reg		yt_sel;
reg		auto_display_pos;

reg	[10:0]	box_h_offset_bin,box_l_offset_bin,h_total_reg_bin,l_total_reg_bin;
reg	[7:0]	current_depth,current_depth_t,current_depth_c;

wire	[7:0]	yt_depth;
wire		color_restore_receiver;
//reg	[7:0]	unit_r_adj,unit_g_adj,unit_b_adj;
/************************************************/
//		行列偏移表
/************************************************/	
/*swsr_2kw8_1kr16_tdp H_L_table(
	.clka(sclk),
	.wea(table_wea),
	.addra(table_addra),
	.dina(table_dina),
	.douta(table_douta),
	.clkb(sclk),
	.web(0),
	.addrb({1'b0,h_num_local[8:0]}),
	.dinb(0),
	.doutb(table_doutb)
	);*/
//swsr_1kw8_512r16_tdp	H_L_table(
//	.clock_a ( sclk ),
//	.wren_a ( table_wea ),
//	.address_a ( table_addra ),
//	.data_a ( table_dina ),
//	.q_a ( table_douta ),
//	
//	.clock_b ( sclk ),
//	.wren_b ( 0 ),
//	.address_b ( {h_num_local[8:0]} ),
//	.data_b ( 0 ),
//	.q_b ( table_doutb )
//	);
//	
/************************************************/
//		参数设置
/************************************************/
always	@(posedge sclk or negedge resetb)
	if (resetb==0)
		begin
			display_num<=0;
			box_h_offset_bin<=0;
			box_l_offset_bin<=0;
			h_total_reg_bin <= 32;
			l_total_reg_bin <= 64;
			pixel_mode<=0;
			no_vs_set<=0;
			light_on_protect<=1;
			auto_display_pos<=0;
			unit_r_adj<=9'h0100;
			unit_g_adj<=9'h0100;
			unit_b_adj<=9'h0100;
			unit_w_adj<=9'h0100;
			color_restore_type<=2'b00;
			local_depth <= 0;
			local_depth_en_t <= 0;
			cas_depth_adj <= 0;
		end
	else if (set_d_ok==1)
		case (set_addr)
			16'h0100:	box_h_offset_bin[7:0]<=set_data;
			16'h0101:	box_h_offset_bin[10:8]<=set_data;
			16'h0102:	box_l_offset_bin[7:0]<=set_data;
			16'h0103:	box_l_offset_bin[10:8]<=set_data;
			16'h0104:	h_total_reg_bin[7:0] <= set_data;
			16'h0105:	h_total_reg_bin[10:8]<= set_data[2:0];          
			16'h0106:	l_total_reg_bin[7:0] <= set_data;
			16'h0107:	l_total_reg_bin[10:8]<= set_data[2:0];  
			16'h0108:	begin
			                if(set_data==0)                 //与未加多屏功能的配置文件保持兼容
			                        display_num<= 0; 	
			                else 
			                	display_num<=set_data-1;
			                end		     
			16'h0209:	local_depth <= set_data;  
			16'h020a:	begin
			                if(set_data == 8'h3C)                 //与未加多屏功能的配置文件保持兼容
			                        local_depth_en_t <= 1; 	
			                else 
			                	local_depth_en_t <= 0;
			                end		     
			16'h020b:	begin
			                if(set_data == 8'h3C)                 //与未加多屏功能的配置文件保持兼容
			                        cas_depth_adj <= 1; 	
			                else 
			                	cas_depth_adj <= 0;
			                end		     
			16'h1032:	color_restore_type<=set_data[1:0];
			16'h1040:	pixel_mode<=set_data[1:0];
			16'h1050:	no_vs_set<=set_data;
			16'h1053:	light_on_protect<=set_data[0];
			
			16'h1099:	auto_display_pos<=set_data[0];	//L9使用，位置自适应使能
			
			16'h7000:	unit_r_adj <= set_data + 1;
			16'h7001:	unit_g_adj <= set_data + 1;
			16'h7002:	unit_b_adj <= set_data + 1;
			16'h7003:	unit_w_adj <= set_data + 1;		
		endcase

assign  vir_pixel=pixel_mode[0];

always	@(posedge sclk)
	if (cas_depth_adj == 1)
		local_depth_en <= 1;
	else
		local_depth_en <= local_depth_en_t;

always	@(posedge sclk)
	if (set_d_ok==1 && (set_addr[15:12]==4'h5 && set_addr[11:10]==2'h1))
		table_wea<=1;	
        else
                table_wea<=0;

assign  table_addra={set_addr[9:0]};
assign  table_dina=set_data;
assign  color_restore_receiver=color_restore_type[1];
/************************************************/
//		自适应截取处理
/************************************************/
always	@(posedge sclk or negedge resetb)
	if(resetb==0)
		current_depth_t<=0;
	else if (data_en_pre==1 && mac_count==PACK_DEPTH_FIRST)
		current_depth_t<=mac_data;
		
always	@(posedge sclk or negedge resetb)
	if(resetb == 0)
		current_depth_c <= 0;
	else if(mac_flag == 0 && mac_error == 0)
		current_depth_c <= current_depth_t;	//接收卡1为0，接收卡2为1……
	
always	@(posedge sclk)
	if(local_depth_en == 0)
		current_depth <= current_depth_c - 1;
	else
		current_depth <= local_depth - 1;
	
always	@(posedge sclk)
	if(yt_sel==1)
	begin
		box_h_offset	<={yt_depth[7:0],3'b000};
		box_l_offset	<=0;
		h_total_reg	<=8;
		l_total_reg	<=1024;
	end
	else if(auto_display_pos==1)
	begin
		box_h_offset	<={current_depth[7:0],3'b000};
		box_l_offset	<=0;
		h_total_reg	<=8;
		l_total_reg	<=1024;
	end
	else
	begin
		box_h_offset	<=box_h_offset_bin;
		box_l_offset	<=box_l_offset_bin;
		h_total_reg	<=h_total_reg_bin;
		l_total_reg	<=l_total_reg_bin;	
	end	
/************************************************/
//		数据计数
/************************************************/
always	@(posedge sclk)
	if (mac_flag==0)
		mac_count<=0;
	else if (mac_count[10]==0)
		mac_count<=mac_count+1;
						
/************************************************/
//		帧包处理
/************************************************/
always	@(posedge sclk)
	if (mac_flag==0)
		zdec_vsout<=0;
	else if (mac_count==PACK_TYPE_FIRST && mac_data==8'hFA && (mac_vendor==1 || mac_vendor==3))
		zdec_vsout<=1;

/************************************************/
//		即时显示设置参数提取
/************************************************/
always	@(posedge sclk)
	if (mac_flag==0)
		set_en_pre<=0;
	else if (mac_count==PACK_TYPE_FIRST && mac_data==8'hF0 && (mac_vendor==1 || mac_vendor==3))
		set_en_pre<=1;

always@(posedge sclk)
	set_count<=mac_count-SET_PACK_OFFSET;

always@(posedge sclk)
        if(set_count[10:4]==display_num)
                set_en<=1'b1;
        else
                set_en<=1'b0;

always@*
        set_count_a_low<=set_count[3:0]-1;
                
always@(posedge sclk )
	w_light<='h0100;

always	@(posedge sclk or negedge resetb)
	if (resetb==0)
		begin
			state		<=64;
			state_2		<=0;
			contrast_adj	<=64;
			display_l_offset<=0;
			display_h_offset<=0;
			r_light<='h0100;
			g_light<='h0100;
			b_light<='h0100;
			led_light<='hff;
		end
	else if (set_en_pre==1 && set_en==1) begin
		case (set_count_a_low[3:0])
			0:	state		<=mac_data;
			1:	state_2		<=mac_data;
			3:	begin
			        if (mac_data!=0) 
					contrast_adj	<=mac_data;
				else
				        contrast_adj    <=64;        
				end
			4:	led_light<=mac_data;
			5:	r_light<=mac_data+1;
			6:	g_light<=mac_data+1;
			7:	b_light<=mac_data+1;
			8:	display_l_offset[7:0]	<=mac_data;
			9:	display_l_offset[10:8]	<=mac_data[2:0];
			10:	display_h_offset[7:0]	<=mac_data;
			11:	display_h_offset[10:8]	<=mac_data[2:0];
			15:	testmode<=mac_data;
		endcase
	end
	
assign	vir_disp=state[7];

//always@*
//	vir_mode<=0;
//always	@(posedge sclk)
//	if (vir_pixel==1 && vir_disp==1)
//		vir_mode<=1;
//	else
//		vir_mode<=0;
/************************************************/
//		行列偏移计算
/************************************************/
//mult_16_16	box_h_offset_mult(
//	.dataa ( {5'h0,box_h_offset} ),
//	.datab ( {14'h0,vir_mode,~vir_mode} ),
//	.result ( h_offset_mult_result )
//	);        
//always	@(posedge sclk)	
//        if(vir_mode==1)
//                h_offset_mult_result<={box_h_offset[9:0],1'b0};                
//        else
//                h_offset_mult_result<={1'b0,box_h_offset[9:0]};
//        
//assign  box_h_offset_local=h_offset_mult_result[10:0];

always	@(posedge sclk)
//	h_start<=box_h_offset_local[10:0]+display_h_offset;
	h_start<=box_h_offset+display_h_offset;
//	
//always	@(posedge sclk)	  	
//        if(table_doutb[15]==1)          //兼容处理
//                h_l_offset<=0;
//        else
//                h_l_offset<=table_doutb[9:0];

//always	@(posedge sclk)
//        l_offset<=box_l_offset;//+h_l_offset;	//L9不用
//
//mult_16_16 box_l_offset_mult(
//        .dataa({5'h0,l_offset}), 
//        .datab({14'h0,vir_mode,~vir_mode}), 
//        .result(l_offset_mult_result)
//        );
//
//assign  box_l_offset_local=l_offset_mult_result[10:0];

always	@(posedge sclk)
//	l_start<=box_l_offset_local[10:0]+display_l_offset;	
	l_start<=box_l_offset+display_l_offset;
//
//mult_16_16 h_total_mult(
//        .dataa({5'h0,h_total_reg}), 
//        .datab({14'h0,vir_mode,~vir_mode}), 
//        .result(h_total_mult_result)
//        );

assign  h_total=h_total_reg;//{h_total_mult_result[10:1],h_total_mult_result[0]|vir_mode};

//
//mult_16_16 l_total_mult(
//        .dataa({5'h0,l_total_reg}), 
//        .datab({14'h0,vir_mode,~vir_mode}), 
//        .result(l_total_mult_result)
//        );

assign  l_total=l_total_reg;//{l_total_mult_result[10:1],l_total_mult_result[0]|vir_mode};
		
/************************************************/
//	显示数据包解析本地行号，总列号计算
/************************************************/
always	@(posedge sclk)
	if (mac_flag==0)
		data_en_pre<=0;
	else if (mac_count==PACK_TYPE_FIRST && mac_data==8'hF5 && (mac_vendor==1 || mac_vendor==3))	//UDP
		data_en_pre<=1;
		
always	@(posedge sclk or negedge resetb)
	if(!resetb)
	begin
		h_num_rec<=0;
		col_max<=0;
		l_num_rec<=0;
	end
	else if (data_en_pre==1)
		case (mac_count)
			HPACK_ADDR_START:	h_num_rec[7:0]<=mac_data;//{1'b0,mac_data}-h_start[7:0];		//UDP
			HPACK_ADDR_START+1:	h_num_rec[11:8]<=mac_data[3:0];//mac_data-h_start[10:8]-h_num_rec[8];	//UDP
			HPACK_ADDR_START+2:	l_num_rec[7:0]<=mac_data;//{1'b0,mac_data}-l_start[7:0];
			HPACK_ADDR_START+3:	l_num_rec[12:8]<=mac_data[4:0];//mac_data-l_start[10:8]-l_num_rec[8];
			HPACK_ADDR_START+4:	col_max[7:0]<=mac_data;
			HPACK_ADDR_START+5:	col_max[12:8]<=mac_data[4:0];
		endcase

always	@(posedge sclk)
	h_max_local<=h_start+h_total;

always	@(posedge sclk)	
	h_num_local<=h_count-h_start;//{h_num_rec[11:9],h_num_rec[7:0]};
always	@(posedge sclk)
	l_max_local<=l_start+l_total;	

/************************************************/
//	显示数据包一行总像素数计算 col_max/3
/************************************************/
always	@(posedge sclk or negedge resetb)
	if(!resetb)
		l_byte_max<=6144;	//MAX
	else if(data_en_pre==1 && mac_count==PACK_DATA_START)	//2个pixel后缓存最大l_pixel_max的值
		l_byte_max<=col_max[12:0];

always	@(posedge sclk or negedge resetb)
	if(!resetb)
		valid_l_byte_max<=0;
	else
		valid_l_byte_max<=l_byte_max-21;
     
/************************************************/
//		本地输出
/************************************************/
always	@(posedge sclk)
	if (mac_flag==0)
		h_active<=0;
	else if (data_en_pre==1 && mac_count==PACK_LENGTH_HIGH)
		h_active<=1;

always@(posedge sclk or negedge resetb)
	if(resetb==0)
		h_count<=0;
	else if(data_en_pre==1 && mac_count==PACK_LENGTH_HIGH)
		h_count <= h_num_rec;
	else if(l_count_byte==l_byte_max-1)
		h_count<=h_count+1;

always@(posedge sclk or negedge resetb)
	if(resetb==0)
		l_count_byte<=0;
	else if(data_en_pre==1 && mac_count==PACK_LENGTH_HIGH)
		l_count_byte <= l_num_rec;
	else if(l_count_byte==l_byte_max-1)
		l_count_byte<=0;
	else if(h_active!=0)
		l_count_byte<= l_count_byte + 1'b1;
		
always@(posedge sclk)
	begin
		l_start_byte	<={l_start[10:0],1'b0}+l_start;
		l_max_local_byte<={l_max_local[10:0],1'b0}+l_max_local;
	end

always@(posedge sclk)
	if(mac_flag==0)
		hsout_a<=0;
	else if(l_count_byte>=l_start_byte && 
		(l_count_byte<l_max_local_byte && l_count_byte<valid_l_byte_max) && 
		h_active==1 && 
		(h_count>=h_start && h_count<h_max_local))
		hsout_a <= 1;
	else
		hsout_a <= 0;

always	@(posedge sclk) 
	begin
		hsout_b<=hsout_a;
		zdec_rec_hout<=hsout_b;
	end

always	@(posedge sclk or negedge resetb)
	if(resetb==0)
		zdec_l_num<=0;
	else if(hsout_a==1 && hsout_b==0)
		zdec_l_num<=l_count_byte-l_start_byte-1;

//mult_16_16 h_num_mult(
//        .dataa({5'h0,h_num_temp}), 
//        .datab({14'h0,~vir_mode,vir_mode}), 
//        .result(h_num_mult_result)
//        );

//assign	h_num_temp=h_num_local-vir_mode;	
//assign	h_num_temp=h_count-vir_mode;
assign	h_num_temp=h_count-h_start;//-vir_mode;
//assign  zdec_h_num=h_num_mult_result[11:1];
always@(posedge sclk or negedge resetb)
	if(resetb==0)
		zdec_h_num<=0;
	else if(hsout_a==1 && hsout_b==0)
		zdec_h_num<=h_num_temp;//h_num_mult_result[11:1];

/************************************************/
//		对比度调整
/************************************************/
always	@(posedge sclk)
	if (mac_data[7]==1)
		contrast_da<=mac_data[6:0];
	else
		contrast_da<=~mac_data[6:0];

always	@(posedge sclk) begin
	flag_da<=mac_data[7];
	flag_db<=flag_da;
	end
	           
mult_9X9_sync	adjust_contrast (
	.clock ( sclk ),
	.dataa ( {1'b0,contrast_adj} ),
	.datab ( {2'h0,contrast_da} ),
	.result ( contrast_db )
	);

always	@(posedge sclk)
	if (flag_db==1) begin
		if (contrast_db[14:13]>0)
			zdec_rec_dout<=8'hff;
		else
			zdec_rec_dout<={1'b1,contrast_db[12:6]};
		end
	else if (contrast_db[14:13]>0)
		zdec_rec_dout<=8'h0;
	else
		zdec_rec_dout<={1'b0,~contrast_db[12:6]};

/************************************************/
//		亮度调整处理
/************************************************/
	        
//*****************软件设置亮度及开屏亮度数据***************//	
always	@(posedge sclk)
        if(init_mode==1'b1)
                cascade_light<=9'h100;
	else if (set_en_pre==1 && set_en==1 && set_count_a_low[3:0]==4'b0100) begin
	        if(mac_data==0)
	                cascade_light<=0;
	        else if(color_restore_receiver==1)//子控使用色彩还原时，分控对亮度不做处理
	        	cascade_light<=9'h100;
	        else
		        cascade_light<=mac_data+1;
		end
		
always	@(posedge sclk)
        if(black==1'b1)
                start_light<=8'h00;
        else if(light_on_protect==1'b0)
                start_light<=8'h80;
        else if(start_light[7]==0 && (local_vs_negedge==1'b1 || (mac_flag==0 && zdec_vsout==1)))
                start_light<=start_light+1;
                
//*****************数据调整写控制***************//
mult_9X9  sys_adj_mult(
	.dataa(cascade_light), 		//9'h100
	.datab(start_light), 		//8'h80
	.result(sys_adj_result)); 	//16'h8000
	
mult_16_16  adj_r_mult(
	.dataa(unit_r_adj), 		//9'h100
	.datab(sys_adj_result), 	//16'h8000	
	.result(adj_result_r));		//24'h800000

assign	r_adj_out = adj_result_r[23:8];

mult_16_16  adj_g_mult(
	.dataa(unit_g_adj), 		//9'h100
	.datab(sys_adj_result), 	//16'h8000	
	.result(adj_result_g));		//24'h800000

assign	g_adj_out = adj_result_g[23:8];

mult_16_16  adj_b_mult(
	.dataa(unit_b_adj), 		//9'h100
	.datab(sys_adj_result), 	//16'h8000	
	.result(adj_result_b));		//24'h800000

assign	b_adj_out = adj_result_b[23:8];

mult_16_16  adj_w_mult(
	.dataa(unit_w_adj), 		//9'h100
	.datab(sys_adj_result), 	//16'h8000	
	.result(adj_result_w));		//24'h800000

assign	w_adj_out = adj_result_w[23:8];

always@(posedge sclk)
	unit_color_adj <={w_adj_out,b_adj_out,g_adj_out,r_adj_out};

//*******************写控制****************************//
always@(posedge sclk)
        test_vs_r<=test_vs;

always@(posedge sclk)
        if((test_vs_r==0 && test_vs==1) && (local_test_en==1'b1 || local_picture_en==1'b1))
                local_vs_negedge<=1;
        else
                local_vs_negedge<=0;
                
assign 	zdec_l_total_byte_t={l_total[10:0],1'b0}+l_total;

always@(posedge sclk)
 if(zdec_l_total_byte_t<valid_l_byte_max)
		zdec_l_total_byte<=zdec_l_total_byte_t;
	else	
		zdec_l_total_byte<=valid_l_byte_max;
/************************************************/
//		YT数据解码
/************************************************/
/*
wire		yt_vsout,yt_rec_hout;
wire	[7:0]	yt_rec_dout;
wire	[11:0]	yt_l_num;
wire	[11:0]	yt_h_num;
wire	[11:0]	yt_l_start_byte,yt_l_end_byte,yt_h_start,yt_h_end;
wire	[11:0]	yt_l_total_byte;                                
assign yt_l_start_byte	=l_start_byte;
assign yt_l_end_byte	=l_max_local_byte;
assign yt_h_start	=h_start;
assign yt_h_end		=h_max_local;

yt_data_rec yt_data_rec(
		.resetb(resetb),
		.sclk(sclk),
		
		.mac_flag(mac_flag),
		.mac_data(mac_data),         
		.mac_error(mac_error),
		.mac_vendor(mac_vendor),//0:无效包 1：中庆 2：YT
		.yt_vs_pre(yt_vs_pre),

		.l_start_byte(yt_l_start_byte),
		.l_end_byte(yt_l_end_byte),
		.h_start(yt_h_start),
		.h_end(yt_h_end),
		
		.vsout(yt_vsout),
		.rec_hout(yt_rec_hout),
		.rec_dout(yt_rec_dout),
		.l_num(yt_l_num),
		.h_num(yt_h_num),
		
		.yt_depth(yt_depth),
		.yt_l_total_byte(yt_l_total_byte)
		);


always@(posedge sclk or negedge resetb)
	if(!resetb)
		yt_sel<=0;
	else if(yt_vsout==1)
		yt_sel<=1;
	else if(zdec_vsout==1)
		yt_sel<=0;


assign vsout		=(yt_sel==1)?yt_vsout:zdec_vsout;
assign rec_hout		=(yt_sel==1)?yt_rec_hout:zdec_rec_hout;
assign rec_dout		=(yt_sel==1)?yt_rec_dout:zdec_rec_dout;
assign l_num		=(yt_sel==1)?yt_l_num:zdec_l_num;
assign rec_h_num	=(yt_sel==1)?yt_h_num:zdec_h_num;
assign l_total_byte	=(yt_sel==1)?yt_l_total_byte:zdec_l_total_byte;

*/
assign vsout		=zdec_vsout;
assign rec_hout		=zdec_rec_hout;
assign rec_dout		=zdec_rec_dout;
assign l_num		=zdec_l_num;
assign rec_h_num	=zdec_h_num;
assign l_total_byte	=zdec_l_total_byte;

/************************************************/
//		UDP行数据转换
/************************************************/
always@(posedge sclk)
begin
	rec_hout_t<=rec_hout;
	hsout<=hout_t1;
end

always@(posedge sclk or negedge resetb)
	if(resetb==0)
		dis_buf_waddr<=0;
	else if(rec_hout==0)//(hsout_b==1 && rec_hout==0)
		dis_buf_waddr<=l_num;
	else if(rec_hout==1)
		dis_buf_waddr<=dis_buf_waddr+1;

assign	dis_buf_we=rec_hout;
assign	dis_buf_wdata=rec_dout;
assign	dout=dis_buf_rdata;

swsr_4kw8_4kr8_dp	date_rec_buf(
	.wrclock ( sclk ),
	.wren ( dis_buf_we ),
	.wraddress ( dis_buf_waddr ),
	.data ( dis_buf_wdata ),
	.rdclock ( sclk ),
	.rdaddress ( dis_buf_raddr ),
	.q ( dis_buf_rdata )
	);

always@(posedge sclk or negedge resetb)
	if(resetb==0)	
		send_out_flag<=0;
	else if(dis_buf_waddr>=l_total_byte && rec_hout==0 && rec_hout_t==1)
		send_out_flag<=1;
	else
		send_out_flag<=0;

always@(posedge sclk or negedge resetb)
	if(resetb==0)	
		hout_t1<=0;
	else if(send_out_flag==1)
		hout_t1<=1;
	else if(dis_buf_raddr==l_total_byte-1)
		hout_t1<=0;

always@(posedge sclk or negedge resetb)
	if(resetb==0)	
		dis_buf_raddr<=0;	
	else if(hout_t1==0)
		dis_buf_raddr<=0;
	else if(dis_buf_raddr<l_total_byte)
		dis_buf_raddr<=dis_buf_raddr+1;
			
always@(posedge sclk or negedge resetb)
	if(resetb==0)	
		h_num<=0;
	else if(send_out_flag==1)
		h_num<=rec_h_num;
/************************************************/
//		测试信号
/************************************************/
/************************************************/
//	显示数据包解析本地行号，总列号计算
/************************************************/
reg		data_en_pre_t;

always	@(posedge sclk)
	data_en_pre_t <= data_en_pre;

always	@(posedge sclk)
	if (zdec_vsout == 1)
		dis_p_count <= 0;
	else if (data_en_pre_t == 0 && data_en_pre == 1 && dis_p_count[5] == 0)
		dis_p_count <= dis_p_count + 1;
		
assign	tout=0;

endmodule		

